import bean.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LeetCode199 {
    public static void main(String[] args) {

    }

    public List<Integer> rightSideView(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<>();

        if (root == null){
            return result;
        }

        ArrayList<TreeNode> last = new ArrayList<>();
        last.add(root);
        while (!last.isEmpty()){
            result.add(last.get(last.size() - 1).val);
            ArrayList<TreeNode> newLevel = new ArrayList<>();
            for (TreeNode node: last){
                if (node.left != null){
                    newLevel.add(node.left);
                }

                if (node.right != null){
                    newLevel.add(node.right);
                }
            }

            last.clear();
            last.addAll(newLevel);
            newLevel.clear();

        }

        return result;
    }
}
